<template>
    <div class="homeBox">
        <el-container direction="vertical" class="el-container">
            <el-header class="homeHeader">
                <div class="title">人事管理系统</div>
               <div>
                   <el-button icon="el-icon-bell" type="text" style="margin-right: 8px;color: #409eff;" size="normal" @click="goChat"/>
                   <el-dropdown class="userInfo" @command="commandHandler"><!--@command="commandHandler" 点击菜单项触发的事件回调-->
                       <span class="el-dropdown-link">
                         {{ user.name }}<i><img :src="user.userface" alt=""></i>
                    </span>
                       <el-dropdown-menu slot="dropdown" size="medium">
                           <!--disabled:禁用的    divided:有分隔线-->
                           <el-dropdown-item command="userInfo">个人中心</el-dropdown-item>
                           <el-dropdown-item command="setting">设置</el-dropdown-item>
                           <el-dropdown-item divided command="logout">注销登录</el-dropdown-item>
                       </el-dropdown-menu>
                   </el-dropdown>
               </div>
            </el-header>
            <el-container>
                <el-aside width="200px">
                    <el-menu
                            background-color="#545c64"
                            text-color="#fff"
                            active-text-color="#ffd04b" router unique-opened>
                        <!--active-text-color="#ffd04b" @select="menuClick">-->
                        <el-submenu :index="index+''" v-for="(item,index) in router"
                                    v-if="!item.hidden" :key="index">  <!--这个是遍历取出index.js中的路由地址数组-->
                            <template slot="title">
                                <i :class="item.iconCls" style="color: cornflowerblue;padding-right: 10px"></i>
                                <span>{{ item.name }}</span>
                            </template>
                            <el-menu-item :index="child.path" v-for="(child,index) in item.children" :key="index"
                                          style="padding-left: 45px">
                                {{child.name }}
                            </el-menu-item>
                        </el-submenu>
                    </el-menu>
                </el-aside>
                <el-main>
                    <el-breadcrumb separator-class="el-icon-arrow-right"
                                   v-if="this.$router.currentRoute.path!=='/home'">
                        <div class="brand">
                            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                            <!--通过this.$router.currentRoute.name:获取当前路由对象的name属性-->
                            <el-breadcrumb-item>{{this.$router.currentRoute.name}}</el-breadcrumb-item>
                        </div>
                    </el-breadcrumb>
                    <div v-if="this.$router.currentRoute.path==='/home'" class="homePage">
                        欢迎来到人事管理系统 !
                    </div>
                    <router-view class="homeRouterView"/>
                </el-main>
            </el-container>
        </el-container>
    </div>
</template>

<script>

    export default {
        name: "Home",
        data() {
            return {
                //将json字符串转换成json对象
                user: JSON.parse(window.sessionStorage.getItem("user"))
            }
        },

        computed: {
            router() {
                return this.$store.state.routes;
            }
        }
        ,
        methods: {
            //去聊天界面
            goChat(){
                this.$router.push("/chat");
            },
            commandHandler(cmd) {
                if (cmd === 'logout') {
                    this.$confirm('此操作注销登录, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.getRequest("/logout");
                        window.sessionStorage.removeItem("user");
                        //注销登录后将store里的菜单数据清空
                        this.$store.commit("initRoutes", []);
                        this.$router.push("/")
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
                }
            }
        }
    }
</script>

<style>

    .homeRouterView {
        margin-top: 15px;
    }

    .brand {
        font-size: 20px;
        font-family: 华文楷体;
    }

    .homePage {
        font-size: 50px;
        font-family: 华文楷体;
        color: black;
        padding-top: 100px;
        text-align: center;
    }

    .homeHeader {
        background-color: #2c3e50;
        display: flex; /*是字体居中*/
        align-items: center; /*竖轴上居中*/
        justify-content: space-between; /*空白的地方在中间*/
        padding: 0 15px;
        box-sizing: border-box;
    }

    .title {
        padding: 3px;
        text-align: left;
        font-size: 30px;
        font-family: 华文行楷;
        color: lightblue;
    }

    .userInfo {
        text-align: right;
        cursor: pointer; /*改变手势*/
    }


    .el-dropdown-link {
        cursor: pointer;
        color: darkturquoise;
        font-size: 19px;
        font-family: 华文楷体;
        display: flex;
        align-items: center;
    }

    .el-dropdown-link img {
        width: 48px;
        height: 48px;
        border-radius: 24px;
        margin-left: 8px;
    }

    /*调整布局充满*/
    html,
    body,
    .homeBox,
    .el-container {
        padding: 0px;
        margin: 0px;
        height: 100%;
    }

</style>